<?php
namespace app\index\controller;
use app\common\controller\HomeBase;



class Index extends  HomeBase
{


	
	public function _initialize()
	{
		parent::_initialize();
		
	}
	
   public function index(){

  
   	
   	
   	$uid=is_login();
 
   //热门小组列表
   $hotgrouplist=parent::$commonLogic->getDataList('group',['status'=>1],true,'sort desc,membercount desc,topiccount desc',false,'','',6);
   $this->assign('hotgrouplist',$hotgrouplist);
   	
   	if($uid>0){
   		
   	
   		
   		
   	}

   	$hottopiclist=parent::$commonLogic->getDataList('topic',['m.status'=>1],'m.*,user.nickname,user.userhead,group.name,group.cover_id','m.update_time desc,m.view desc,m.choice desc',false,[['user','user.id=m.uid','LEFT'],['group','group.id=m.tid']],'',6);
   	foreach ($hottopiclist as $k =>$v){
   		 
   		$comment=model('comment')->where(['fid'=>$v['id']])->order('create_time desc')->limit(1)->select();
   		if($comment){
   			$hottopiclist[$k]['ccreate_time']=$comment[0]['create_time'];
   			$hottopiclist[$k]['cuid']=$comment[0]['uid'];
   		}
   		 
   	}
   	
   	
   	
   	$this->assign('hottopiclist',$hottopiclist);
   	
   	
   	//为你推荐4
   	$tjgrouplist=parent::$commonLogic->getDataList('group',['status'=>1,'choice'=>1],true,'update_time desc',false,'','',4);
   	$this->assign('tjgrouplist',$tjgrouplist);
   	//最新话题5
   	$newtopiclist=parent::$commonLogic->getDataList('topic',['status'=>1],true,'create_time desc',false,'','',5);
   	$this->assign('newtopiclist',$newtopiclist);
   	
   	
   	return $this->fetch();
   	
   }
   public function yzemailurl($id){
   	if (is_login()==0) {
   		 
   		$this->error('亲！请登录',url('index/login'));
   	} else {
   		$uid = is_login();
   		
   		
   		$user=parent::$commonLogic->getDataInfo('user',['id'=>$uid]);
   
   		if($id==md5($user['salt'].$uid.$user['usermail'])){
   			if($user['status']<3){
   				 
   				
   				parent::$commonLogic->setDataValue('user',['id'=>$uid],'status',2);
   			}else{
   				
   				parent::$commonLogic->setDataValue('user',['id'=>$uid],'status',5);
   			}

   			$this->success('验证成功',url('user/index'));
   			 
   			 
   		}else{
   			$this->error('非法验证',url('user/index'));
   		}
   
   	}
   	 
   	 
   	 
   	 
   }
   public function yzemail(){
   	 
   	$mail=$this->request->param();
   	
   	$uid =  is_login();
   	$user=parent::$commonLogic->getDataInfo('user',['id'=>$uid]);
   
   	$emailinfo=parent::$commonLogic->getDataInfo('user',['usermail'=>$mail['email'],'id'=>array('neq',$uid)]);
   	if($emailinfo){
   		return json(array('code'=>0,'msg'=>'该邮箱已经被其他账号注册'));
   	}else{
   		$n['usermail']=$mail['email'];
   		$n['id']=$uid;
   		parent::$commonLogic->dataEdit('user',$n,false);
   		
   		 
   		
   		$data['email']=$mail['email'];
   		$data['title']='邮箱验证';
   		$str=md5($user['salt'].$uid.$data['email']);
   		$data['body']='您的链接已经生成<br>http://'.$_SERVER['HTTP_HOST'].url('index/yzemailurl').'?id='.$str;
   		
   	
   		
   		asyn_sendmail($data);
   		return json(array('code'=>1,'msg'=>'邮箱登录已更改为新邮箱，请到邮箱查收验证'));
   	}

   	 
   	 
   	 
   	 
   	 
   	 
   	 
   }
   public function forgetcodebymail(){
   	 
   	$mail=$this->request->param();
	 
   	$emailinfo=parent::$commonLogic->getDataInfo('user',['usermail'=>$mail['email']]);
   	//是否能得到
   	
   	
   	if($emailinfo){

   		 
   		$data['email']=$mail['email'];
   		$data['title']='忘记密码-邮箱验证';
 
   		 
   		$code=generate_code($mail['email']);
   		$data['body']='您的验证码已经生成<br>'.$code;
   		asyn_sendmail($data);
   		return json(array('code'=>1,'msg'=>'验证码已经发送，请到邮箱查收验证'));
   		
   	}else{
   		return json(array('code'=>0,'msg'=>'该邮箱不存在'));
  
   		 
   		 
   		 
   	}
   
   }
   public function reyzemail(){
   
   	$mail=$this->request->param();
   	$uid =  is_login();
   	$user=parent::$commonLogic->getDataInfo('user',['id'=>$uid]);
   	 
       
        $emailinfo=parent::$commonLogic->getDataInfo('user',['usermail'=>$mail['email'],'id'=>array('neq',$uid)]);
        if($emailinfo){
        	return json(array('code'=>0,'msg'=>'邮箱已被使用'));
        }else{
        	$n['usermail']=$mail['email'];
        	if($user['status']==2){
        		$n['status']=1;
        	}else{
        		$n['status']=3;
        	}
        	$n['id']=$uid;
        	parent::$commonLogic->dataEdit('user',$n,false);
        	
        	 
        	$data['email']=$mail['email'];
        	$data['title']='邮箱验证';
        	$str=md5($user['salt'].$uid.$data['email']);
        	$data['body']='您的链接已经生成<br>http://'.$_SERVER['HTTP_HOST'].url('index/yzemailurl').'?id='.$str;
        	asyn_sendmail($data);
        	return json(array('code'=>1,'msg'=>'邮箱登录已更改为新邮箱，请到邮箱查收验证'));
        	
        	
        	
        }

   }
   
   public function send_mail()
   {
   	 
   
   	$mail=$this->request->param();
   
   	$res=send_email($mail['email'], $mail['title'], $mail['body']);
   
   	if($res==1){
   		return json(array('code'=>1,'msg'=>'邮件已发送，请到邮箱进行查收'));
   		//	$this->success('邮件已发送，请到邮箱进行查收');
   	}else{
   		return json(array('code'=>0,'msg'=>'邮件发送失败，请检查邮箱设置'));
   		//$this->error('邮件发送失败，请检查邮箱设置');
   	}
   }
}
